package com.xh.jf_blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xh.jf_blog.bean.Category;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.mapper.CategoryMapper;
import com.xh.jf_blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2024/2/1/001
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    public R getPage(int page, int limit, Category category) {
        PageHelper.startPage(page, limit);
        List<Category> list = categoryMapper.getAll(category);
        PageInfo<Category> pageInfo = new PageInfo<Category>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    public Category getById(int categoryId) {
        return categoryMapper.selectByPrimaryKey(categoryId);
    }

    public void saveOrUpdate(Category category) {
        if (category.getCategoryId() == null) {
            categoryMapper.insertSelective(category);
        } else {
            categoryMapper.updateByPrimaryKeySelective(category);
        }
    }

    public void deleteById(int categoryId) {
        categoryMapper.deleteByPrimaryKey(categoryId);
    }

    public List<Category> getList() {
        return categoryMapper.getAll(new Category());
    }
}
